import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import Home from '../pages/Home.vue'
import Login from '../pages/Login.vue'
import Parking from '../pages/Parking.vue'
import AICustomerService from "../pages/AICustomerService.vue";
import MeterReading from '../pages/MeterReading.vue'
import BuildingManagement from '../pages/BuildingManagement.vue'
import CommunityManagement from '../pages/CommunityManagement.vue'
import LightManagement from '../pages/LightManagement.vue'
import PatrolManagement from '../patrol/PatrolManagement.vue';
import KnowledgeManagement from "../pages/KnowledgeManagement.vue";
import Index from "../pages/Index.vue";
import TestMan from "../patrol/testMan.vue";



const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    component: Home,redirect:'index',
    children: [
      {
        path: '/news',
        name: 'News',
        component: () => import('../pages/News.vue')
      },
      {
        path: 'parking',
        component: Parking
      },
      {
        path: 'Monitor',
        component: ()=>import('../pages/Monitor.vue')
      },
      {
        path: 'faceInfo',
        component: ()=>import('../pages/faceInfo.vue')
      },
      {
        path: 'faceopencv',
        component: ()=>import('../pages/faceopencv.vue')
      },
      {
        path: 'aics',
        component: AICustomerService
      },
      {
        path: 'meter-reading',
        component: MeterReading
      },
      {
        path: 'parking-record',
        component: () => import('../pages/ParkingRecord.vue')
      },
      {
        path: 'vehicle-list',
        component: () => import('../pages/VehicleList.vue')
      },
      {path:'man',name:'management',component: TestMan},
      {path:'management',name:'patrolManagement',component: PatrolManagement},
      {path:'/plan',name:'patrolPlan',component:()=>import('../patrol/PatrolPlan.vue')},
      {path:'/point',name:'patrolPoint',component:()=>import('../patrol/PatrolPoint.vue')},
      {path:'/category',name:'patrolCategory',component:()=>import('../patrol/PatrolCategory.vue')},
      {path:'/map',name:'patrolMap',component:()=>import('../patrol/PatrolMap.vue')},
      {path:'/record',name:'patrolRecord',component:()=>import('../patrol/PatrolRecord.vue')},
      {path:'/route',name:'patrolRoute',component:()=>import('../patrol/PatrolRoute.vue')},
      {path:'/task',name:'patrolTask',component:()=>import('../patrol/PatrolTask.vue')},
      {path:'analyze',name:'patrolAIAnalyze',component:()=>import('../patrol/PatrolAIAnalyze.vue')},
      {
        path: 'building-management',
        component: BuildingManagement
      },
      {
        path: 'community-management',
        component: CommunityManagement
      },
      {
        path: 'light-management',
        component: LightManagement
      },
      {
        path: '/knowledge-management',
        component: KnowledgeManagement
      },
      {
        path: '/index',
        name: 'index',
        component: Index
      },
      {path:'/repair',name:'repair',component:()=>import('../pages/Repair.vue')},
      {path:'/sensitiveWord-management',name:'sensitiveWord-management',component:()=>import('../pages/SensitiveWordManagement.vue')},
    ]
  },


]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 加入路由守卫 控制页面跳转 未登录情况强制跳转到登陆页面
// 需求： 如果用户没有登录 不允许访问其他页面 强制跳转到login页面
router.beforeEach((to, from, next) => {
  let token = sessionStorage.getItem('token')
  if (to.path === '/login') {
    next()
  } else {
    // 判断用户有没有登录
    if (!token) {
      // 没有token 不允许路由 强制跳转到login
      ElMessage.error("请先登录")
      next({
        name: 'login'
      })
    } else {
      next()
    }
  }
})

// 3、导出路由
export default router
